<?php
class FormAction extends CommonAction {
	//过滤查询字段
	function _filter(&$map){
		$keywords = trim($_POST['name']);
                $where['title'] =  array('like',"%".$keywords."%");
                $where['content'] =  array('like',"%".$keywords."%");
                $where['_logic'] = "or";
                $map['_complex'] = $where;
		//$map['title'] = array('like',"%".trim($_POST['name'])."%");
		if ($_POST['cid']) {
			$map['cid'] = $_POST['cid'];
		}
	}
	// 节点临时存取数组，避免多次调用数据库
	protected $tree = array();
	// 节点查询结果存储
	protected $child = array();
	// 子节点查询
	protected function __findChild($parent=0) {
		$result = array();
		foreach ($this->tree as $v) {
			if ($parent == $v['pid']) {
				$result[] = $v;
			}
		}
		// 显示每个子节点
		foreach ($result as $v) {
			$this->child[] = $v['id'];
			$this->__findChild($v['id']);
		}
	}
	public function catedel() {
		// 只接受ajax传入
		if (1 !=$_REQUEST['ajax']) {
			return;
		}
		$cate = M('Formcate');
		$this->tree = $cate->field('id,pid,name')->where('status=1 ')->findAll();
		$this->__findChild($_REQUEST['cid']);
		$cids = implode(',', $this->child);
		$form = D('Form');
		if (!empty($cate)) {
			if (isset($_REQUEST['cid'])) {
				$condition['id'] = $cids ? array('in', $cids.','.$_REQUEST['cid']) : $_REQUEST['cid'];
				$condition2['cid'] = $condition['id'];
				if (false !== $cate->where($condition)->delete()) {
					$form->where($condition2)->setField('cid', '0'); // 删除栏目的同时将栏目下的文章放入根栏目
					$this->ajaxReturn($_REQUEST['cid'], '删除成功', 1);
				} else {
					$this->ajaxReturn($_REQUEST['cid'], '删除失败', 0);
				}
			}
		}
		$this->ajaxReturn($_REQUEST['cid'], '删除出错',0);
	}
	public function cateedit() {
		// 只接受ajax传入
		if (1 !=$_REQUEST['ajax']) {
			return;
		}
		$cate = M('Formcate');
		if (NULL != ($_POST['pcid'])) {
			$data = array(
				'name' => $_REQUEST['cname'],
				'pid' => $_REQUEST['pcid'],
				'user_id' => $_SESSION[C('USER_AUTH_KEY')],
				'create_time' => time(),
				'update_time' => 0,
				'status' => 1,
			);
			if (false === $nid = $cate->data($data)->add()) {
				$this->ajaxReturn($data,'新增出错',0);
			}
			$nodeid = ereg_replace('xtree-', '', $_POST['nodeid']);
			$this->ajaxReturn($nodeid.','.$nid, '新增成功', 1);
		}
		elseif (NULL != ($_POST['cid'])) {
			$data['name'] = $_REQUEST['cname'];
			$data['update_time'] = time();
			$where['id'] = $_REQUEST['cid'];
			$cate->where($where)->save($data);
			$this->ajaxReturn($_POST['nodeid'].','.$_POST['cid'], '修改成功', 2);
		}
		else {
			$this->ajaxReturn(0, '发生错误', 0);
		}
	}
	public function _before_index() {
		$this->__cate_list();
		$this->assign('useAjax',1);
	}
	public function _before_edit() {
		$this->__cate_list('edit');
	}
	public function _before_add() {
		$this->__cate_list('add');
	}
	public function _before_upload(&$options){
		$options['saveRule'] = 'time';
		$options['savePath'] =  C('FORM_UP_DIR'); // 设置附件上传目录
		$options['maxSize']  = 3145728 ; // 设置附件上传大小
		$options['allowExts']  = array('jpg', 'gif', 'png', 'jpeg', 'txt', 'doc', 'docx', 'pdf', 'xls', 'xlsx'); // 设置附件上传类型
	}
	protected function __cate_list($m) {
		switch ($m) {
			case index:
				$method = 1;
				break;
			case edit:
				$method = 2;
				break;
			case add:
				$method = 3;
				break;
			default:
				$method = 1;
				break;
		}
		$outform = '<input type=\'hidden\' id=\'tm-xtree-2\' name=\'cid\' value=\'0\' />'."\n"; // 根节点id从2开始计算
		$outjs = 'if (document.getElementById) {'."\n";
		$outjs .= 'var tree = new WebFXTree("All","__URL__");'."\n";
		//添加节点
		$cate = M('Formcate');
		$vo = $cate->order('id asc')->findAll();
		$try = 2;  // 根节点id从2开始计算
		foreach ($vo as $k => $v) {
			$try++;
			if (0 == $v['pid']) {
				if (1 == $method) {
					$outjs .= 't'.$v['id'].'=tree.add(new WebFXTreeItem("'.$v['name'].'","'.__URL__.'/index/cid/'.$v['id'].'"));'."\n";
				}
				elseif (2 == $method) {
					$outjs .= 't'.$v['id'].'=tree.add(new WebFXTreeItem("'.$v['name'].'","javascript:chooseNode('.$try.','.$v['id'].')"));'."\n";
				}
				elseif (3 == $method) {
					$outjs .= 't'.$v['id'].'=tree.add(new WebFXTreeItem("'.$v['name'].'","javascript:chooseNode('.$try.','.$v['id'].')"));'."\n";
				}
			}
			else {
				if (1 == $method) {
					$outjs .= 't'.$v['id'].'=t'.$v['pid'].'.add(new WebFXTreeItem("'.$v['name'].'","'.__URL__.'/index/cid/'.$v['id'].'"));'."\n";
				}
				elseif (2 == $method) {
					$outjs .= 't'.$v['id'].'=t'.$v['pid'].'.add(new WebFXTreeItem("'.$v['name'].'","javascript:chooseNode('.$try.','.$v['id'].')"));'."\n";
				}
				elseif (3 == $method) {
					$outjs .= 't'.$v['id'].'=t'.$v['pid'].'.add(new WebFXTreeItem("'.$v['name'].'","javascript:chooseNode('.$try.','.$v['id'].')"));'."\n";
				}
			}
			$outform .= '<input type=\'hidden\' id=\'tm-xtree-'.$try.'\' name=\'cid\' value=\''.$v['id'].'\' />'."\n";
		}
		$outjs .= "document.write(tree);}\ntree.expandAll();\n";
		$this->assign('genJs', $outjs);
		$this->assign('genForm', $outform);
	}

        public function   insert($filed = 'title') {
        parent::insert($filed);
    }
}
?>